package com.fitns.user.controller.baseConfig;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fitns.user.service.baseConfig.TbMiddleLedgerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.fitns.user.conf.Resp;
import com.fitns.user.dto.baseConfig.TbMiddleLedgerDTO;
import com.fitns.user.dto.baseConfig.TbMiddleLedgerVO;
import com.fitns.user.utils.ExcelUtils;
import com.fitns.user.utils.UuidUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@CrossOrigin //解决前后端对接跨域问题
@RequestMapping("backend")
@Api(description="中轉站台賬台账")
public class TbMiddleLedgerController {

	
	@Autowired
	private TbMiddleLedgerService tbMiddleLedgerService;
	
	@PostMapping("insert/middle")
	@ApiOperation("新增中转站台账")
	public Object insertMiddleLedger(@Validated TbMiddleLedgerDTO tbMiddleLedgerDTO) {
	Integer insert	 =  tbMiddleLedgerService.insertMiddleLedger(tbMiddleLedgerDTO);
		Resp resp = null;
		if(insert!=0) {
			resp = Resp.wrap("中转账台账添加成功");
		}else {
			resp = Resp.errorWrap("中转账添加失败");
		}
		return resp;
	}
	
	@PutMapping("update/middle")
	@ApiOperation("修改中转站台账")
	 public Object updateMiddleLedger(@Validated TbMiddleLedgerDTO tbMiddleLedgerDTO) {
		Integer update =  tbMiddleLedgerService.updateMiddleLedger(tbMiddleLedgerDTO);
		 Resp resp = null;
		 if(update != 0) {
			resp =  Resp.wrap("修改数据成功");
		 }else {
			 resp = Resp.errorWrap("修改数据失败");
		 }
		 return resp;
	 }


	
	@DeleteMapping("delete/middleId")
	@ApiOperation("根据id删除中转站台账")
	 public Object deleteMiddleLedgerById(@RequestParam long id) {
			Integer delete =  tbMiddleLedgerService.deleteMiddleLedgerById(id);
			 Resp resp = null;
			 if(delete != 0 ) {
				resp =  Resp.wrap("删除数据成功");
			 }else {
				 resp = Resp.errorWrap("删除数据失败");
			 }
			 return resp;
		 }
	
	@GetMapping("select/middle")
	//@PostMapping("select/middle")
	@ApiOperation("查询中转站台账")
    public Object selectMiddleLedger( TbMiddleLedgerDTO tbMiddleLedgerDTO) {
        PageInfo<TbMiddleLedgerVO> bannerId = tbMiddleLedgerService.selectMiddleLedger(tbMiddleLedgerDTO);
        bannerId.setPageSize(tbMiddleLedgerDTO.getPageSize());
        return Resp.page(bannerId);
    }
	
	@ApiOperation("根据id中转账信息")
	@GetMapping("select/middleById")
	public Object selectMiddleById(long id) {
		return tbMiddleLedgerService.selectMiddleById(id);
	}
	
	
	
	@GetMapping("export/middle")
    @ApiOperation("exclue导出中转站台账，8月8号原型修改，取消了中转站导出功能")
	 public void exportCustomer(TbMiddleLedgerDTO tbMiddleLedgerDTO, HttpServletResponse response, HttpServletRequest request) throws Exception{
        List<List<String>> excelList =tbMiddleLedgerService.export(tbMiddleLedgerDTO);
        //String str="new sheet";
        ExcelUtils.export(response,request,"中转站台账",excelList);
    }

	
	 /** 图片上传功能 */ 
	//postman测试以数组进行，用相同的key也就是传多个pic，不同的pic有不同的图片
    @ApiOperation("上传图片")
    @PostMapping("uplodeFile")
    public String insertImage(TbMiddleLedgerDTO tbMiddleLedgerDTO, @RequestParam("pic") MultipartFile[] pic) throws Exception {
        String images="";
        // 把图片保存到图片目录下
        for (int i=0;i<pic.length;i++) {
            MultipartFile file=pic[i];
            // 为每个文件生成一个新的文件名
            String picName = UuidUtils.getUUID();
            // 取文件扩展名
            String oriName = file.getOriginalFilename();
            String extName = oriName.substring(oriName.lastIndexOf("."));
            // 保存文件
            //file.transferTo(new File(Constants.IMAGE_PATH + picName + extName));
            file.transferTo(new File("D:\\workspace\\woliu-zhhw\\zhhw\\src\\main\\resources\\image\\"+ picName + extName));
           
            images+=picName + extName+";";
        }

        if(images!=""){
            images=images.substring(0,images.length()-1);
        }
        // 把文件名保存到数据库
        tbMiddleLedgerDTO.setImage(images);
        tbMiddleLedgerService.inserIcon(tbMiddleLedgerDTO);
        return tbMiddleLedgerDTO.getImage();
    }
}
